<style>
button[i="close"] {
	display: inline-block;
	width: 50px;
} 
</style>

<div class="bomb" id="companyDia" style="padding: 15px;">
	<form method="post" id="assetForm">
		<div class="companyDetail" id="companyDetail">
			<input type="hidden" name="companyId" id="companyId" /> <input
				type="hidden" name="id" value="[#if asset??]${asset.id!}[/#if]" />
			<input type="hidden" name="attachfile" id="assetImage"
				value="[#if asset??]${asset.attachfile!}[/#if]" />
			<div class="form-group" style="margin-bottom: 5px;margin-left: 30px;">
				<button type="button" id="assetFileUpload"
					style="width: auto; padding: 5px 10px;color: white;">上传图片</button>
				<label class="control-label"
					style="margin-top: 5px; color: #6e7b8e;">支持JPG、PNG、GIF、BMP格式的图片文件，图片大小不能超过5M！</label>
			</div>
			<div>
				<div
					style="margin: 15px 30px; padding: 5px; border: 2px dashed #ebebeb; display: inline-block; width: auto;float: left;">
					<a href="javascript:void(0)"> <img style="margin-right: 0px;max-height: 200px;max-width: 200px;"
						alt="Responsive image"
						src="[#if asset??]${asset.attachfile!}[#else]/res/images/respective/company/addZi_03.png[/#if]"
						id="showAsset"/>
					</a>

				</div>
				<div style="float: left;margin-top: 15px;">
					<div style="height: 130px;">
					<div class="form-controlAzt ppSelect" style="width: 300px;">
					<select style="height: 100%;" onchange="changeLevel();" required
						id="assetType" value="" name="name">
						<option value="">请选择资质类型</option>
							[#if purAssesTypes??]
							<optgroup label="采购商资质">
							[#list purAssesTypes as pur]
							<option value="${pur.code!}"
								_childrenOption="[#if pur.children??][#list pur.children as c]<option value='${c.code}' [#if asset?? && asset.level?? && asset.level == c.code]selected[/#if]>${c.name!}</option>[/#list][/#if]"
								[#if asset?? && asset.name==pur.code]selected[/#if]>${pur.name!}</option>
							[/#list]
							</optgroup>
							[/#if]
								[#if proAssesTypes??]
								<optgroup label="供应商资质">
								[#list proAssesTypes as pro]
								<option value="${pro.code!}"
									_childrenOption="[#if pro.children??][#list pro.children as c]<option value='${c.code}' [#if asset?? && asset.level?? && asset.level == c.code]selected[/#if]>${c.name!}</option>[/#list][/#if]"
									[#if asset?? && asset.name==pro.code]selected[/#if]>${pro.name!}</option>
								[/#list]
								</optgroup>
								[/#if]
					</select>
				</div> 
				<div class="form-controlAzt ppSelect"
					style="margin-top: 20px;width: 300px;margin-top: 15px;display: block;margin-bottom: 20px;" id="levelDiv">
					<select style="height: 100%;" id="level" value="" required  name="level"  >
						<option value="">请选择资质级别</option>
					</select>
				</div>
				 <div style="margin-top: 15px;">
				 	<label class="control-label detailLab" style="margin-top: 15px;">公开状态:</label>
				 	<input type="radio" name="display" id="display1" value="0" [#if asset?? && asset.display==0]checked[/#if]><label for="display1">不公开</label>
				 	<input style="margin-left: 15px;" type="radio" name="display" id="display2" value="1" [#if asset?? && asset.display==1]checked[/#if][#if !asset??]checked[/#if]><label for="display2">开放浏览</label>
				 </div>  
				 </div>
				<div style="margin-top:30px;"> 
					<button type="button" class="btn btn-info" style="width: 100px; padding: 3px 15px;"
						onclick="saveAsset();">保存</button>
					<button type="reset"
						class="btn btn-default" style="width: 100px; padding: 3px 15px;margin-left: 20px;"
						onclick="layer.closeAll();return false;">取消</button>
				</div>
				</div>   
				
			</div>
		</div>
	</form>
</div>

<script type="text/javascript">

    $(function(){
    	$("#assetFileUpload , #showAsset").on('click' , function(){
			layer.fileManager({callback : function(data){
				if(data.length > 0){
					$("#showAsset").attr("src" , data[0].url);
					$("#assetImage").val(data[0].url);
				}
			}});
		});
    })


    function changeLevel(){
        var option = $("#assetType option:selected").attr("_childrenOption");
        if(option != undefined){
            $("#level").html("<option value=''>请选择资质级别</option>");
            if(option.length > 0){
                $("#level").append(option);
                $("#levelDiv").show();
            }else{
                $("#levelDiv").hide();
            }
        }
    }

    changeLevel();

	var canSave = true;

    function saveAsset(){

    	if(!canSave){
    		return false;
		}

		canSave = false;

        var validate = true;

        /*js验证*/

		$("#assetForm select").each(function(){
			if($(this).is(":visible") && $(this).attr("required") !== undefined && $(this).val().trim() == ""){
				layer.tips('不能为空，请填写！', $(this) , {tips: [2 ,'#fb4047']});
				$(this).focus();
				validate = false;
				canSave  = true;
				return false;
			}
		});


		if(validate && $("#assetImage").val() == "" ){
            layer.tips('请上传资质图片！', $("#showAsset") , {tips: [2 ,'#fb4047']});
            $("#showAsset").focus();
            validate = false;
        }

        if(!validate){
			canSave = true;
            return false;
        }



        /*验证结束*/

        if(validate){
			var lding = layer.load(2);
            $('#companyId').val($("#company_id").val());
            $.ajax({
                url :  '/company/saveAsset' ,
                type : 'post' ,
                dataType : 'json' ,
                data : $("#assetForm").serialize(),
                success : function(data){
					layer.close(lding);
                    if(data.success){
                        layer.closeAll();
						layer.msg("企业资质保存成功！" , {time:1000 } , function(){
							location.reload();
						});
                    }else{
						layer.msg(data.msg , {time:1000});
                    }
					canSave = true;
                }
            });
        }
    }

</script>